//
//  EVCPopInputView.m
//  EverVCiOSClient
//
//  Created by 张可可 on 15/11/12.
//  Copyright (c) 2015年 EverVC. All rights reserved.
//

#import "EVCPopInputView.h"
#import "NSString+TextSize.h"
#import "Masonry.h"
#import "NSString+Check.h"


#define InputViewAddHeight 5

@interface EVCPopInputView ()<UITextViewDelegate>


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewLayoutForBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputContentViewLayoutForHeight;
///  默认40；
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLalbelBottomLayoutForSubTitle;



@property (nonatomic, strong) CallBackWithSelectedIndex callBackBlock;
@end

@implementation EVCPopInputView

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (EVCPopInputView *)popInputViewWithTitle:(NSString *)title
                                  subTitle:(NSString *)subTitle
                                 placehold:(NSString *)placehold
                                    preset:(NSString *)preset {
    
    EVCPopInputView *popInputView = [EVCPopInputView getViewFromXIBWtihName:@"EVCPopInputView"];
    
    popInputView.titleLabel.text = title;
    popInputView.subTitleLabel.text = subTitle;
    popInputView.placeholdLabel.text = placehold;
    popInputView.inputTextView.text = preset;
    if (preset.isHaveStr) {
        popInputView.placeholdLabel.hidden = YES;
    }
    
    
    return popInputView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.bgView.hidden = NO;
    self.bgView.alpha = 0.3f;
    self.bgView.frame = ScreenBounds;
    self.sendBtn.enabled = NO;
    self.contentView.alpha = 1;
    [self bringSubviewToFront:self.contentView];
    self.titleLabel.text = self.subTitleLabel.text = @"";
    
    
    self.inputContentViewLayoutForHeight.constant = 55 + InputViewAddHeight;
    self.inputContentView.layer.borderColor = UIColor.k_hexColor(0xdfdfdf).CGColor;
    self.inputContentView.layer.borderWidth = 0.5f;
    self.inputTextView.textContainerInset = UIEdgeInsetsMake(3, 5, 0, 5);
    
    self.inputTextView.layoutManager.allowsNonContiguousLayout = NO;
    self.inputTextView.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    
    self.titleLabel.textColor = UIColor.k_t2Color;
    self.subTitleLabel.textColor = UIColor.k_t2Color;
    self.placeholdLabel.textColor = UIColor.k_t4Color;
    self.inputTextView.textColor = UIColor.k_t2Color;
    [self.cancelBtn setTitleColor:UIColor.k_t3Color forState:UIControlStateNormal];
    self.sendBtn.k_titleColor(UIColor.k_t1Color);
    
}

- (IBAction)tapGAction:(id)sender {
    
    [self removeFromSuperview];
}

#pragma mark - Action
//  取消
- (IBAction)didClickCancelBtnAction:(id)sender {
    [self hide];
    if (self.callBackBlock) {
        self.callBackBlock(0, nil);
    }
}

//  发送
- (IBAction)didClickSendBtnAction:(id)sender {
    
    if (self.isNeedLimitTextContent == YES && [NSString isNickName:self.inputTextView.text] == NO) {
       NSString *warning = @"请输入2~4位汉字昵称";
        [MBProgressHUD showTipMessageInView:warning timer:1.0f];
        return;
    }
    
    [self hide];
    if (self.callBackBlock) {
        self.callBackBlock(1, self.inputTextView.text);
    }
}

- (void)clickButtonAction:(CallBackWithSelectedIndex)btnActionBlock {
    self.callBackBlock = btnActionBlock;
}
#pragma mark - TextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    
    if (self.inputTextView.text.length > 0) {
        self.placeholdLabel.hidden=YES;
        self.sendBtn.enabled = YES;
        [self.sendBtn setTitleColor:UIColor.k_t1Color forState:UIControlStateNormal];
    }
    else{
        self.placeholdLabel.hidden=NO;
        self.sendBtn.enabled = NO;
        [self.sendBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    if (self.nulTextCanSend == YES) {
        self.sendBtn.enabled = YES;
        [self.sendBtn setTitleColor:UIColor.k_t1Color forState:UIControlStateNormal];
    }
    
    [self adjustInputViewHeight];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (self.limitTextNum > 0 && [text isEqualToString:@""] == NO) {
        if ((textView.text.length + text.length) > self.limitTextNum) {
            return NO;
        }
    }
    return YES;
}

- (void)adjustInputViewHeight {
    
    NSString *str = self.inputTextView.text;
    CGFloat strWidth = ScreenSize.width - 16 - 20;
    CGFloat textHeight = [str textHeightWithWidth:strWidth fontSize:15];
    if (self.subTitleLabel.text.isHaveStr == YES) {
        self.titleLalbelBottomLayoutForSubTitle.constant = 8;
    } else {
        self.titleLalbelBottomLayoutForSubTitle.constant = 0;
    }
    
    if (textHeight > 45) {
        
        if (textHeight > 80) {
            self.inputContentViewLayoutForHeight.constant = 80 + InputViewAddHeight;
        } else {
            self.inputContentViewLayoutForHeight.constant = textHeight+20;
        }
        
    } else {
        self.inputContentViewLayoutForHeight.constant  = 69 + InputViewAddHeight;
    }
    
    [self layoutIfNeeded];
}


- (void)show {
    
    UIWindow *window = kWindow;
    self.frame = ScreenBounds;
    self.alpha = 0;
    self.bgView.alpha = 0;
    [self adjustInputViewHeight];
    [window addSubview:self];
    [self layoutIfNeeded];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(window);
        
    }];
    [self.inputTextView becomeFirstResponder];
    
}
- (void)hide {
    
    [self.inputTextView resignFirstResponder];
    self.contentViewLayoutForBottom.constant = -300;
    [UIView animateWithDuration:0.25 animations:^{
//        [UIView setAnimationCurve:curve];
        self.bgView.alpha = 0;
        [self layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

#pragma mark - Keyboard events

- (void)keyboardWillShown:(NSNotification*)aNotification
{
    
    CGRect keyboardRect = [[aNotification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        keyboardRect = CGRectMake(keyboardRect.origin.x, keyboardRect.origin.y, MAX(keyboardRect.size.width,keyboardRect.size.height), MIN(keyboardRect.size.width,keyboardRect.size.height));
    }
    
    
    
    UIViewAnimationCurve curve = [[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];

    
    double duration = [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    
    
//    self.hidden = NO;
    self.contentViewLayoutForBottom.constant = keyboardRect.size.height;
    [UIView animateKeyframesWithDuration:duration delay:0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
        [UIView setAnimationCurve:curve];
        self.alpha = 1;
        self.bgView.alpha = 0.3;
        [self layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
    }];
//    [UIView animateWithDuration:duration animations:^{
//    } completion:^(BOOL finished) {
//        
//        
//    }];
    
    
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
//    UIViewAnimationCurve curve = [[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    double duration = [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        //        [UIView setAnimationCurve:curve];
        
        self.contentViewLayoutForBottom.constant = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
        
    }];

}

- (void)setNulTextCanSend:(BOOL)nulTextCanSend {
    
    _nulTextCanSend = nulTextCanSend;
    
    if (_nulTextCanSend == YES) {
        self.sendBtn.enabled = YES;
    }
}


@end
